<?php


namespace export;

use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use \PHPExcel_Style_NumberFormat;    //设置列的格式==>>设置文本格式

class Export
{
    /**
     * 导出并下载
     * @param $newExcel
     * @param $filename
     * @param $format
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    public function downloadExcel($data, $excelTitle, $filename, $format):array
    {
        // $format只能为 Xlsx 或 Xls
//        if ($format == 'Xlsx') {
//            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//        } elseif ($format == 'Xls') {
//            header('Content-Type: application/vnd.ms-excel');
//        }
//
//        header("Content-Disposition: attachment;filename="
//            . $filename . date('Y-m-d') . '.' . strtolower($format));
//        header('Cache-Control: max-age=0');


        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        // sheet title
        $sheet->setTitle($filename);
        //设置表头
        for ($i = 0; $i < count($excelTitle);$i++){
            $sheet->setCellValueByColumnAndRow($i+1, 1, $excelTitle[$i]);
        }
        //设置表格内容
        if(!empty($data)){
            foreach ($data as $k=>$datum){
                for ($i = 0; $i < count($excelTitle);$i++ ){
                    $sheet->setCellValueByColumnAndRow($i+1, $k+2, $datum[$i] );
                }
            }
        }

//        $startRow = 1; // 数据起始行
//        foreach ($data as $key => $value) {
//            foreach ($excelTitle as $k => $v) {
//                $column = Coordinate::stringFromColumnIndex($k + 1);
//                if ($key == 0) {
//                    $sheet->setCellValue($column . $startRow, $v);
//                    $sheet->getColumnDimension($column)->setWidth(20);
//                }
//                $i = $key + 2; //表格是从2开始的
//                //dd($v);
//                $sheet->setCellValue($column . $i, $value[$k]);
//            }
//        }

        $objWriter = IOFactory::createWriter($spreadsheet, $format);
        //设置导出保存目录
        $date = date('Y-m-d');
        $url = request()->domain()."/export/".$date.'/'.$filename.'.'.$format;
        $dir = app()->getRootPath() ."public/export/".$date.'/';
        $mode = 0777;//最高权限
        if (is_dir($dir) == false) {//如果路径不存在，则创建
            @mkdir($dir, $mode);
        }

        $objWriter->save($dir.$filename.'.'.$format);
        return ['url'=>$url];
        //通过php保存在本地的时候需要用到
        //$objWriter->save($dir.'/demo.xlsx');

        //以下为需要用到IE时候设置
        // If you're serving to IE 9, then the following may be needed
        //header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        //header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        //header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        //header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        //header('Pragma: public'); // HTTP/1.0
    }
}